package com.dandelion.business.controller;


import cn.hutool.json.JSONUtil;
import com.dandelion.business.entity.SysMenu;
import com.dandelion.business.entity.vo.login.resp.Menu;
import com.dandelion.business.entity.vo.menu.req.MenuReqVO;
import com.dandelion.business.entity.vo.user.CurrentUser;
import com.dandelion.business.entity.vo.validationgroup.BaseInsert;
import com.dandelion.business.entity.vo.validationgroup.BaseUpdate;
import com.dandelion.business.service.ISysMenuService;
import com.dandelion.framework.annotations.CUAnnotation;
import com.dandelion.framework.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@Api(tags = "菜单模块")
@Slf4j
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private ISysMenuService menuService;


    /**
     * 菜单列表
     *
     * @param currentUser 当前用户
     * @return {@link R}<{@link List}<{@link Menu}>>
     */
    @ApiOperation(value = "我的菜单",notes = "我的菜单列表,前端加载页面时候使用")
    @PostMapping("/list")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<List<Menu>> menuList(@ApiIgnore @CUAnnotation CurrentUser currentUser) {
        log.info("我的菜单,用户id:{}", currentUser.getUserId());
        return menuService.menuList(currentUser.getUserId());
    }

    @ApiOperation("新增菜单")
    @PostMapping("/add")
    //@PreAuthorize("hasAuthority('settings:menu:add')")
    public R<Boolean> addMenu(
            @ApiIgnore @CUAnnotation CurrentUser currentUser,
            @Validated(value = {BaseInsert.class}) @RequestBody MenuReqVO req
    ) {
        log.info("新增菜单,用户信息:{},参数:{}", JSONUtil.toJsonStr(currentUser), JSONUtil.toJsonStr(req));
        return menuService.add(currentUser, req);
    }

    @ApiOperation("获取菜单")
    @GetMapping("/query")
    //@PreAuthorize("hasAuthority('settings:menu:add')")
    public R<SysMenu> queryMenu(Long id) {
        log.info("获取菜单,参数:{}", id);
        return menuService.queryMenu(id);
    }

    @ApiOperation("修改菜单")
    @PostMapping("/edit")
    //@PreAuthorize("hasAuthority('settings:menu:add')")
    public R editMenu(
            @ApiIgnore @CUAnnotation CurrentUser currentUser,
            @Validated(value = {BaseUpdate.class}) @RequestBody MenuReqVO req
    ) {
        log.info("修改菜单,参数:{}", JSONUtil.toJsonStr(req));
        return menuService.edit(currentUser, req);
    }

}
